package server

import (
	"context"
	"exam/rpc/initiate/mysql"
	"exam/rpc/model"
	user "exam/rpc/proto"
)

func UserRegister(ctx context.Context, in *user.UserRegisterReq) (*user.UserRegisterResp, error) {
	u := model.User{
		Name:     in.Name,
		Password: in.Password,
	}
	err := mysql.DB.Create(&u).Error
	if in.Name == u.Name {
		return &user.UserRegisterResp{Greet: "当前用户名已存在"}, err
	}
	if err != nil {
		return &user.UserRegisterResp{Greet: "注册失败"}, err
	}
	return &user.UserRegisterResp{Greet: "注册成功"}, err
}

func UserList(ctx context.Context, in *user.UserListReq) (*user.UserListResp, error) {
	u := &[]model.User{}
	err := mysql.DB.Find(&u).Error
	if err != nil {
		return &user.UserListResp{List: nil}, err
	}
	return &user.UserListResp{List: nil}, err
}
